import {Toast} from "antd-mobile";

const request = async (method: string, url: string, data?: any) => {
    try {
        // 开始请求
        const res = await fetch("/api/front" + url, {
            // 请求地址
            method: method.toLocaleUpperCase(),
            // 请求头配置
            headers: {
                'Content-Type': "application/json",
            },
            // 将json数据转换为字符串
            body: JSON.stringify(data),
        });
        // 获取响应信息
        const resData = await res.json();
        const {code, msg} = resData.data;
        // 请求报错
        if (code > 201) {
            await Promise.reject(msg)
        }
        return resData.data.data;
    } catch (err) {
        // 错误请求执行后
        Toast.show({
            content: err as string
        })
    }
}

export default request;